﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Simulator.Properties;

namespace Simulator
{
	public partial class Form1 : Form
	{
		public Form1 ()
		{
			InitializeComponent ();

			// For proper scaling, we must set the font _after_ calling InitializeComponent.
			var font = new Font ("Segoe UI", 9);
			if (font.OriginalFontName != font.Name)
			{
				// Segoe UI doesn't exist on the system. We're probably on Windows XP. Let's fallback to Tahoma.
				font.Dispose ();
				font = new Font ("Tahoma", 9);
			}
			this.Font = font;
		}

		protected override void OnLoad (EventArgs e)
		{
			base.OnLoad (e);

			if (Settings.Default.CallUpgrade)
			{
				Settings.Default.Upgrade ();
				Settings.Default.CallUpgrade = false;
				Settings.Default.Save ();
			}

			this.Location = Settings.Default.FormLocation;
			this.Size = Settings.Default.FormSize;
			this.WindowState = Settings.Default.FormWindowState;
			this.splitContainer1.SplitterDistance = Settings.Default.FormSplitter1Distance;
			this.splitContainer2.SplitterDistance = Settings.Default.FormSplitter2Distance;
			this.splitContainerPropertyGrids.SplitterDistance = Settings.Default.FormSplitterPropGridsDistance;
		}

		protected override void OnFormClosing (FormClosingEventArgs e)
		{
			base.OnFormClosing (e);

			Settings.Default.FormWindowState = this.WindowState;
			if (this.WindowState == FormWindowState.Normal)
			{
				Settings.Default.FormLocation = this.Location;
				Settings.Default.FormSize = this.Size;
			}
			else
			{
				Settings.Default.FormLocation = this.RestoreBounds.Location;
				Settings.Default.FormSize = this.RestoreBounds.Size;
			}

			Settings.Default.FormSplitter1Distance = this.splitContainer1.SplitterDistance;
			Settings.Default.FormSplitter2Distance = this.splitContainer2.SplitterDistance;
			Settings.Default.FormSplitterPropGridsDistance = this.splitContainerPropertyGrids.SplitterDistance;

			Settings.Default.Save ();
		}
	}
}
